package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.AnimalResourceBean;

import java.util.List;

import static com.mingtu.guards.utils.MyConstant.ItemDecorationSpaces;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class BioAdapter extends BaseQuickAdapter<AnimalResourceBean.PageBean.ListBean, BaseViewHolder> {
    private Context context;

    public BioAdapter(Context context) {
        super(R.layout.item_animal_resource);
        this.context = context;
    }


    @Override
    protected void convert(@NonNull BaseViewHolder helper, AnimalResourceBean.PageBean.ListBean listBean) {
        String name = listBean.getName();
        String ldName = listBean.getLdName();
        List<AnimalResourceBean.PageBean.ListBean.ResAllPicsBean> resAllPicsBeans = listBean.getResAllPics();
        String level = listBean.getLevel();
        ImageView ivLevel = helper.getView(R.id.iv_level);
        if (!StringUtils.isEmpty(level)) {
            ivLevel.setVisibility(View.VISIBLE);
            if (level.equals("国家Ⅰ级重点保护")) {
                ivLevel.setBackgroundResource(R.mipmap.icon_animal_level1);
            }
            if (level.equals("国家Ⅱ级重点保护")) {
                ivLevel.setBackgroundResource(R.mipmap.icon_animal_level2);
            }
            if (level.equals("省重点保护")) {
                ivLevel.setBackgroundResource(R.mipmap.icon_animal_level3);
            }
        } else {
            ivLevel.setVisibility(View.GONE);
        }


        if (!StringUtils.isEmpty(name)) {
            helper.setText(R.id.tv_name1, name);
        }

        if (!StringUtils.isEmpty(ldName)) {
            helper.setText(R.id.tv_name2, ldName);
        }
        if (resAllPicsBeans != null) {
            if (resAllPicsBeans.size() > 0) {
                AnimalResourceBean.PageBean.ListBean.ResAllPicsBean resAllPicsBean = resAllPicsBeans.get(0);
                String url = null;
                try {
                    url = resAllPicsBean.getUrl() + "?x-oss-process=image/resize,m_fill,w_320,h_320";
                    Glide.with(context)
                            .asDrawable()
                            .thumbnail(0.6f)
                            .load(url)
                            .apply(BaseApplication.getRequestOptions())
                            .into((ImageView) helper.getView(R.id.iv_picture));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                helper.getView(R.id.iv_picture).setBackgroundResource(R.mipmap.img_place1);
            }
        } else {
            helper.getView(R.id.iv_picture).setBackgroundResource(R.mipmap.img_place1);
        }

        setLayoutParams(helper);

    }

    private void setLayoutParams(BaseViewHolder helper) {
        ViewGroup.LayoutParams lp = helper.getView(R.id.iv_picture).getLayoutParams();
        int screenWidth = ScreenUtils.getScreenWidth(); //照片一行2个，间隙是10dp
        int mWidth = (screenWidth - SizeUtils.dp2px(ItemDecorationSpaces) * 3) / 2;
        int mHeight = mWidth;
        lp.width = mWidth;
        lp.height = mHeight;
        helper.getView(R.id.iv_picture).setLayoutParams(lp);
    }

}
